package com.isunland.managesystem.ui;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListView;
import com.google.gson.Gson;
import com.isunland.managesystem.R;
import com.isunland.managesystem.adapter.LocaleWorkQueryCountSubAdapter;
import com.isunland.managesystem.base.BaseListFragment;
import com.isunland.managesystem.common.ApiConst;
import com.isunland.managesystem.entity.LocaleWorkQueryCount;
import com.isunland.managesystem.entity.LocaleWorkQueryCountSub;
import com.isunland.managesystem.entity.LocaleWorkQueryCountSubListOriginal;
import com.isunland.managesystem.utils.ParamsNotEmpty;
import java.util.ArrayList;
import java.util.HashMap;

/**
 * 对象功能:
 * 开发公司: 北京信普达系统工程有限公司
 * 开发人员: wangyanan
 * 创建时间: 2017/4/8 14:53
 */
public class LocaleWorkQueryCountSubListFragment extends BaseListFragment {
  public static final String EXTRA_ID =
      LocaleWorkQueryCountSubListFragment.class.getSimpleName() + "EXTRA_ID";
  public static final String EXTRA_STATUS =
      LocaleWorkQueryCountSubListFragment.class.getSimpleName() + "EXTRA_STATUS";
  private static final int REQUEST_ADD = 1;
  private static final int REQUEST_DETAIL = 2;
  private String mId;
  private ArrayList<LocaleWorkQueryCountSub> mList;
  private LocaleWorkQueryCountSubAdapter mAdapter;
  @SuppressWarnings("all") private String mStatus;
  private LocaleWorkQueryCount mItemMain;

  public static LocaleWorkQueryCountSubListFragment newInstance(String id, String status,
      LocaleWorkQueryCount itemMain) {
    Bundle bundle = new Bundle();
    bundle.putString(EXTRA_ID, id);
    bundle.putString(EXTRA_STATUS, status);
    bundle.putSerializable(LocaleWorkQueryCountSubDetailFragment.EXTRA_MAIN_ITEM, itemMain);
    LocaleWorkQueryCountSubListFragment fragment = new LocaleWorkQueryCountSubListFragment();
    fragment.setArguments(bundle);
    return fragment;
  }

  public void initView(View view, Bundle savedInstanceState) {
    setTitleCustom(R.string.localeWorkQueryCountSubList);
    setListAdapter(mAdapter);
  }

  public void initData() {
    mId = getArguments().getString(EXTRA_ID);
    mStatus = getArguments().getString(EXTRA_STATUS);
    mItemMain = (LocaleWorkQueryCount) getArguments().getSerializable(
        LocaleWorkQueryCountSubDetailFragment.EXTRA_MAIN_ITEM);
    mList = new ArrayList<>();
    mAdapter = new LocaleWorkQueryCountSubAdapter(mActivity, mList);
  }

  @Override public String getUrl() {
    return ApiConst.URL_QUERY_LIST_LOCALEWORKQUERYCOUNTSUB;
  }

  @Override public HashMap<String, String> getparameters() {
    ParamsNotEmpty params = new ParamsNotEmpty();
    params.put("mainId", mId);
    return params.getParams();
  }

  @Override public void resolveResponse(String json) {
    LocaleWorkQueryCountSubListOriginal original =
        new Gson().fromJson(json, LocaleWorkQueryCountSubListOriginal.class);
    mList.clear();
    mList.addAll(original.getRows());
    mAdapter.notifyDataSetChanged();
  }

  @Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    super.onCreateOptionsMenu(menu, inflater);
    //去掉限制,网页端在主表提交时也可以添加分工明细.
    //if (MyStringUtil.equals(mStatus, DataStatus.NEW) || MyStringUtil.equals(mStatus, DataStatus.NEWBACK)) {
    inflater.inflate(R.menu.menu_add, menu);
    //}
  }

  @Override public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
      case R.id.menu_item_add:
        LocaleWorkQueryCountSubDetailActivity.newInstance(this, mActivity, REQUEST_ADD, mItemMain,
            null, LocaleWorkQueryCountSubDetailFragment.TYPE_ADD);
        return true;

      default:
        return super.onOptionsItemSelected(item);
    }
  }

  @Override public void onListItemClick(ListView l, View v, int position, long id) {
    super.onListItemClick(l, v, position, id);
    if (mAdapter == null) return;
    //避免点击header时的getitem出现-1的情况.
    if (position < mListview.getHeaderViewsCount()) return;
    LocaleWorkQueryCountSub item = mAdapter.getItem(position - l.getHeaderViewsCount());
    LocaleWorkQueryCountSubDetailActivity.newInstance(this, mActivity, REQUEST_DETAIL, mItemMain,
        item, LocaleWorkQueryCountSubDetailFragment.TYPE_DETAIL);
  }

  @Override public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (resultCode != Activity.RESULT_OK) return;
    if (requestCode == REQUEST_ADD) volleyPost();
    if (requestCode == REQUEST_DETAIL) volleyPost();
  }
}
